package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.models.MetaSeoModel;

import entities.Author;
import entities.AuthorQuestion;
import entities.MetaSeo;
import entities.SiteConfig;

import models.AuthorModel;
import models.AuthorQuestionModel;

import systems.ConfigSystem;
import systems.RecentlyAuthorUpdate;

public class LastestUserServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		SiteConfig config = ConfigSystem.getConfigs();
		
		List<Author> authors = RecentlyAuthorUpdate.getAuthors();
		
		//Get Author Question
		List<String> aids = new ArrayList<String>();
		for (int i = 0; i < authors.size(); i++) {
			aids.add(authors.get(i).getId());
		}
		
		AuthorQuestionModel aqModel = AuthorQuestionModel.getInstance();
		Map<String, AuthorQuestion> authorQuestions = aqModel.getByListIds(aids);
		
		//Set seo content
		MetaSeoModel metaseoModel = MetaSeoModel.getInstance();
		MetaSeo metaseo = metaseoModel.get("lastest-user");
		
		request.setAttribute("authors", authors);
		request.setAttribute("authorQuestions", authorQuestions);
		request.setAttribute("metaseo", metaseo);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/lastest_user.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
